//
//  DepositDetailViewController.m
//  ShoppingExperience
//
//  Created by LiYinDi on 15/9/29.
//  Copyright (c) 2015年 RuiBen. All rights reserved.
//

#import "DepositDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "PrintWebViewController.h"

@interface DepositDetailViewController ()//<PrintWebDelegate>
{
    PrintWebViewController *printWebVC;
}
@end

@implementation DepositDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (printWebVC.view!=nil) {
        [printWebVC.view removeFromSuperview];
        [printWebVC removeFromParentViewController];
        
    }

    // Do any additional setup after loading the view from its nib.
    [_detailAlphaView setBackgroundColor:[Global parseColor:WESTORE_TRANSCLUCENT]];
    [_detailAlphaView addTarget:self action:@selector(detailAlphaViewClick) forControlEvents:UIControlEventTouchUpInside];
    [_relateOrderBtn addTarget:self action:@selector(relateOrderBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self requestDepositDetail:_docNumStr];
}
-(IBAction)printBtnClick:(id)sender
{
    if (printWebVC.view!=nil) {
        [printWebVC.view removeFromSuperview];
        [printWebVC removeFromParentViewController];
        
    }
    printWebVC=[[PrintWebViewController alloc] init];
   // printWebVC.delegate=self;
    printWebVC.docNumStr=_docNumStr;
    printWebVC.docTypeStr=@"Deposit";
    printWebVC.docStatusStr=@"";
    printWebVC.view.frame = CGRectMake(0,
                                       0,
                                       self.view.bounds.size.width,
                                       self.view.bounds.size.height);
    
    [self addChildViewController:printWebVC];
    [self.view addSubview:printWebVC.view];
    
    
}
//-(void)PrintWebBackClick
//{
//    if (printWebVC.view!=nil) {
//        [printWebVC.view removeFromSuperview];
//        [printWebVC removeFromParentViewController];
//        
//    }
//}
//
-(IBAction)cancelOrderBtnClick:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(DepositDetailCanCelDepositClick)]) {
        [self.delegate DepositDetailCanCelDepositClick];
    }
}
-(void)detailAlphaViewClick
{
       
    [_detailScrollView setContentOffset:CGPointMake(0, 0)];
//    if ([self.delegate respondsToSelector:@selector(DepositDetailAlphaViewClick)]) {
//        [self.delegate DepositDetailAlphaViewClick];
//    }
    
    if (self.view!=nil) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
        
    }

    
}
-(void)relateOrderBtnClick
{
    // NSLog(@"dddddwerwrwerewr");
    if (_relateOrderNumLabel.text.length>0) {
        
        if ([self.delegate respondsToSelector:@selector(DepositDetailRelateOrderClick)]) {
            [self.delegate DepositDetailRelateOrderClick];
        }

        
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"未关联订单" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
    
}
-(void)requestDepositDetail:(NSString *)numStr
{
       if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        
        [params setObject:numStr forKey:@"DocNum"];
        // NSLog(@"paramsLIST====%@",params);
        [HttpRequest requestData:@"Guide/DepositDetail" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            //NSLog(@"detaillll=%@",severDataDict.dataDict);
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    _detailNumLabel.text=[severDataDict.dataDict objectForKey:@"DocNum"];
                    _cancelDepositBtn.hidden=NO;
                    _printBtn.hidden=NO;
                   // [self resizeLabel:_detailNumLabel];
                    [Global resizeForLable:_detailNumLabel WithText:_detailNumLabel.text parentView:self.view];
                    if ([severDataDict.dataDict objectForKey:@"DocStatus"] !=[NSNull null]) {
                        NSString *statusStr=[severDataDict.dataDict objectForKey:@"DocStatus"];
                        _detailStatusLabel.text=[NSString stringWithFormat:@"【%@】",statusStr];
                        if ([statusStr isEqualToString:@"未收银"]) {
                            _cancelDepositBtn.hidden=NO;
                            _printBtn.hidden=YES;

                            [_detailStatusLabel setTextColor:[Global parseStatusColor:statusStr]];
                        }
                        else if ([statusStr isEqualToString:@"已结算"])
                        {
                            _cancelDepositBtn.hidden=NO;
                            _printBtn.hidden=NO;
                            [_detailStatusLabel setTextColor:[Global parseStatusColor:statusStr]];
                            
                        }
                        
                        else if ([statusStr isEqualToString:@"已取消"])
                        {
                            _cancelDepositBtn.hidden=YES;
                            _printBtn.hidden=YES;
                            
                            [_detailStatusLabel setTextColor:[Global parseStatusColor:statusStr]];
                        }
                        
                       // [self resizeLabel:_detailStatusLabel];
                         [Global resizeForLable:_detailStatusLabel WithText:_detailStatusLabel.text parentView:self.view];
                    }
                    
                    
                    CGRect frame1=_detailStatusLabel.frame;
                    frame1.origin.x=_detailNumLabel.frame.origin.x+_detailNumLabel.frame.size.width;
                    _detailStatusLabel.frame=frame1;
                    
                    CGRect frame=_detailTypeLabel.frame;
                    frame.origin.x=_detailStatusLabel.frame.origin.x+_detailStatusLabel.frame.size.width-2;
                    _detailTypeLabel.frame=frame;
                    
                    if ([severDataDict.dataDict objectForKey:@"DocType"]!=[NSNull null]) {
                        NSString *docTypeStr=[severDataDict.dataDict objectForKey:@"DocType"];
                        _detailTypeLabel.text=[NSString stringWithFormat:@"【%@】",docTypeStr];
                        
                        
                        if ([docTypeStr isEqualToString:@"未使用"]) {
//                            _cancelDepositBtn.hidden=NO;
//                            _printBtn.hidden=YES;
                            [_detailTypeLabel setTextColor:[Global parseStatusColor:docTypeStr]];
                        }
                        else if ([docTypeStr isEqualToString:@"已使用"]) {
                            _cancelDepositBtn.hidden=YES;
                            _printBtn.hidden=NO;

                            [_detailTypeLabel setTextColor:[Global parseStatusColor:docTypeStr]];
                        }
                        
                    }
                    if ([severDataDict.dataDict objectForKey:@"MemberName"]!=[NSNull null]) {
                        _detailMemberNameLabel.text=[severDataDict.dataDict objectForKey:@"MemberName"];
                       // [self resizeLabel:_detailMemberNameLabel];
                         [Global resizeForLable:_detailMemberNameLabel WithText:_detailMemberNameLabel.text parentView:self.view];
                    }
                    if ([severDataDict.dataDict objectForKey:@"GuideName"]!=[NSNull null]) {
                        _detailGuideNameLabel.text=[severDataDict.dataDict objectForKey:@"GuideName"];
                       // [self resizeLabel:_detailGuideNameLabel];
                        [Global resizeForLable:_detailGuideNameLabel WithText:_detailGuideNameLabel.text parentView:self.view];

                        CGRect frame=_detailGuideNameTagLabel.frame;
                        frame.origin.x=_detailMemberNameLabel.frame.origin.x+_detailMemberNameLabel.frame.size.width+15;
                        _detailGuideNameTagLabel.frame=frame;
                        
                        CGRect frame1=_detailGuideNameLabel.frame;
                        frame1.origin.x=_detailGuideNameTagLabel.frame.origin.x+_detailGuideNameTagLabel.frame.size.width+3;
                        _detailGuideNameLabel.frame=frame1;
                        
                        CGRect frame2=_detailStoreNameTagLabel.frame;
                        frame2.origin.x=_detailGuideNameLabel.frame.origin.x+_detailGuideNameLabel.frame.size.width+15;
                        _detailStoreNameTagLabel.frame=frame2;
                        
                    }
                    
                    if ([severDataDict.dataDict objectForKey:@"StoreName"]!=[NSNull null]) {
                        _detailStoreNameLabel.text=[severDataDict.dataDict objectForKey:@"StoreName"];
                       // [self resizeLabel:_detailStoreNameLabel];
                        [Global resizeForLable:_detailStoreNameLabel WithText:_detailStoreNameLabel.text parentView:self.view];
                        CGRect frame2=_detailStoreNameLabel.frame;
                        frame2.origin.x=_detailStoreNameTagLabel.frame.origin.x+_detailStoreNameTagLabel.frame.size.width+3;
                        _detailStoreNameLabel.frame=frame2;
                    }
                   
            
                    if ([severDataDict.dataDict objectForKey:@"DocDate"]!=[NSNull null]) {
                        _detailDateLabel.text=[severDataDict.dataDict objectForKey:@"DocDate"];
                    }
                    if ([severDataDict.dataDict objectForKey:@"DueDate"]!=[NSNull null]) {
                        _detailCashDateLabel.text=[severDataDict.dataDict objectForKey:@"DueDate"];
                    }
                    if ([severDataDict.dataDict objectForKey:@"Subscription"]!=[NSNull null]) {
                        _detailCashAmountLabel.text=[NSString stringWithFormat:@"¥%0.02f",[[severDataDict.dataDict objectForKey:@"Subscription"] floatValue]];
                    }
                    if ([severDataDict.dataDict objectForKey:@"RelationDate"]!=[NSNull null]) {
                        if ([[severDataDict.dataDict objectForKey:@"RelationDate"] length]>0) {
                            _relateOrderDateLabel.text=[severDataDict.dataDict objectForKey:@"RelationDate"];
                            _relateOrderNumLabel.hidden=NO;
                            _relateOrderNumTagLabel.hidden=NO;
                        }
                        else
                        {
                            _relateOrderDateLabel.text=@"无";
                            _relateOrderNumLabel.text=@"";
                            _relateOrderNumLabel.hidden=YES;
                            _relateOrderNumTagLabel.hidden=YES;
                        }
                      //  [self resizeLabel:_relateOrderDateLabel];
                        [Global resizeForLable:_relateOrderDateLabel WithText:_relateOrderDateLabel.text parentView:self.view];
                    }
                    
                    if ([severDataDict.dataDict objectForKey:@"OrderNum"]!=[NSNull null]) {
                        if ([[severDataDict.dataDict objectForKey:@"OrderNum"] length]>0) {
                            _relateOrderNumLabel.text=[severDataDict.dataDict objectForKey:@"OrderNum"];
                        }
                        
                       // [self resizeLabel:_relateOrderNumLabel];
                         [Global resizeForLable:_relateOrderNumLabel WithText:_relateOrderNumLabel.text parentView:self.view];
                        CGRect frame2=_relateOrderNumTagLabel.frame;
                        frame2.origin.x=_relateOrderDateLabel.frame.origin.x+_relateOrderDateLabel.frame.size.width+15;
                        _relateOrderNumTagLabel.frame=frame2;
                        
                        CGRect frame1=_relateOrderNumLabel.frame;
                        frame1.origin.x=_relateOrderNumTagLabel.frame.origin.x+_relateOrderNumTagLabel.frame.size.width+3;
                        _relateOrderNumLabel.frame=frame1;
                    }
                    if ([severDataDict.dataDict objectForKey:@"ItemName"]!=[NSNull null]) {
                        _goodNameLabel.text=[severDataDict.dataDict objectForKey:@"ItemName"];
                        
                    }
                    
                    if ([severDataDict.dataDict objectForKey:@"MinAmount"]!=[NSNull null]&&[severDataDict.dataDict objectForKey:@"MaxAmount"]!=[NSNull null]) {
                        _goodPriceLabel.text=[NSString stringWithFormat:@"%0.02f~%0.02f",[[severDataDict.dataDict objectForKey:@"MinAmount"] floatValue],[[severDataDict.dataDict objectForKey:@"MaxAmount"] floatValue]];
                        
                    }
                    
                    NSString *remarkStr=[severDataDict.dataDict objectForKey:@"Remark"];
                    _detailRemarkView.text=remarkStr;
                    
                    //[Global resizeTextView:_detailRemarkView contentStr: _detailRemarkView.text];
                    CGFloat height=  [Global heightForTextView:_detailRemarkView WithText:_detailRemarkView.text];
                    
                    CGRect frameA=_detailRemarkView.frame;
                    frameA.size.height=height;
                    _detailRemarkView.frame=frameA;
                    
                    CGRect frameD=_cancelOrderRecordView.frame;
                    frameD.origin.y=_detailRemarkView.frame.origin.y+_detailRemarkView.frame.size.height+10;
                    _cancelOrderRecordView.frame=frameD;
                    
                    
                    CGRect frameE=_detailLastView.frame;
                    frameE.size.height=_cancelOrderRecordView.frame.origin.y+_cancelOrderRecordView.frame.size.height+2;
                    _detailLastView.frame=frameE;


                    
                    NSString *cancelDateStr=[severDataDict.dataDict objectForKey:@"CancelDate"];
                    if (cancelDateStr.length >0) {
                        _cancelOrderRecordView.hidden=NO;
                        _cancelOrderDateLabel.text=cancelDateStr;
                        [Global resizeForLable:_cancelOrderDateLabel WithText:_cancelOrderDateLabel.text parentView:self.view];
                        
                        CGRect frameA=_cancelOrderGuideNameTagLabel.frame;
                        frameA.origin.x=_cancelOrderDateLabel.frame.origin.x+_cancelOrderDateLabel.frame.size.width+12;
                        _cancelOrderGuideNameTagLabel.frame=frameA;
                        
                        CGRect frameB=_cancelOrderGuideNameLabel.frame;
                        frameB.origin.x=_cancelOrderGuideNameTagLabel.frame.origin.x+_cancelOrderGuideNameTagLabel.frame.size.width+3;
                        _cancelOrderGuideNameLabel.frame=frameB;

                        NSString *cancelGuideStr=[severDataDict.dataDict objectForKey:@"CancelBy"];
                        _cancelOrderGuideNameLabel.text=cancelGuideStr;
                      //  [self resizeLabel:_cancelOrderGuideNameLabel];
                         [Global resizeForLable:_cancelOrderGuideNameLabel WithText:_cancelOrderGuideNameLabel.text parentView:self.view];
                        CGRect frame1=_cancelDepositTagLabel.frame;
                        frame1.origin.x=_cancelOrderGuideNameLabel.frame.origin.x+_cancelOrderGuideNameLabel.frame.size.width+12;
                        _cancelDepositTagLabel.frame=frame1;
                        
                        CGRect frameC=_cancelDepositLabel.frame;
                        frameC.origin.x=_cancelDepositTagLabel.frame.origin.x+_cancelDepositTagLabel.frame.size.width+3;
                        _cancelDepositLabel.frame=frameC;
                        
                        
                       NSString *RSubscriptionStr=[NSString stringWithFormat:@"%0.02f",[[severDataDict.dataDict objectForKey:@"RSubscription"] floatValue]];
                        _cancelDepositLabel.text=RSubscriptionStr;

                        NSString *CancelRemarkStr=[severDataDict.dataDict objectForKey:@"CancelRemark"];
                        _cancelRemarkLabel.text=CancelRemarkStr;
                    }
                    else
                    {
                        _cancelOrderRecordView.hidden=YES;
                    }

                    NSMutableArray *mDetailPictureArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                    NSMutableArray *detailImageUrlArray=[NSMutableArray array];
                    for (int i=0; i<mDetailPictureArray.count; i++) {
                        
                        [detailImageUrlArray addObject:[[mDetailPictureArray objectAtIndex:i] objectForKey:@"ImageUrl"]];
                    }
                    
                    [self setDetailPicture:detailImageUrlArray];
                    
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    [Global loginViewController:self noteStr:requestResultStr];
                        //    [Global loginViewController:self];
                    }
                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
    
}
-(void)setDetailPicture:(NSMutableArray *)imageArray
{
    
    float viewHeight=0.0f;
    for (int i=0; i<imageArray.count; i++) {
        
        UIButton *imageButton=[UIButton buttonWithType:UIButtonTypeCustom];
        
        CGRect rect=imageButton.frame;
        imageButton.tag=i+1;
        rect.size.height=90;
        rect.size.width=90;
        rect.origin.x=25+(90+25)*(i%5);
        rect.origin.y=17+100*(i/5);
        // NSLog(@"i/6=====%d",i/5);
        imageButton.frame=rect;
        viewHeight=rect.origin.y+rect.size.height+15;
        //  [imageButton addTarget:self action:@selector(imageButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *sampleImageView=[[UIImageView alloc] initWithFrame:imageButton.frame];
        sampleImageView.tag=i+1+100;
        [sampleImageView setImageWithURL:[NSURL URLWithString:[imageArray objectAtIndex:i]]];
        [_detailPictureView addSubview:sampleImageView];
        [_detailPictureView addSubview:imageButton];
        // NSLog(@"rect.origin.x==%f",rect.origin.x);
        
    }
    //NSLog(@"viewHeight===%f",viewHeight);
    CGRect viewFrame=_detailPictureView.frame;
    viewFrame.size.height=viewHeight;
    _detailPictureView.frame=viewFrame;
    
    CGRect viewFrame1=_detailLastView.frame;
    viewFrame1.origin.y=_detailPictureView.frame.origin.y+_detailPictureView.frame.size.height+20;
    _detailLastView.frame=viewFrame1;
    [_detailScrollView setContentSize:CGSizeMake(0, _detailLastView.frame.origin.y+_detailLastView.frame.size.height+10)];
    
    [_detailScrollView setShowsVerticalScrollIndicator:NO];
    [_detailScrollView setScrollEnabled:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
